﻿// CMyVideoCon.cpp: 实现文件
//

#include "stdafx.h"
#include "Item.h"
#include "CMyVideoCon.h"
#include "afxdialogex.h"
#include "CObjSet.h"
CVideoZip zip;
//std::string videoFile = "e:/8d/cf/car.avi";
//std::string infofile = "e:/8d/info.txt";
//std::string zipfile = "e:/8d/resV.avi";
//std::string backfile = "e:/8d/back.jpg";
// CMyVideoCon 对话框

std::string infofile = "C:/Users/Administrator/Desktop/test/info.txt";
std::string zipfile = "C:/Users/Administrator/Desktop/test/resV.avi";
std::string backfile = "C:/Users/Administrator/Desktop/test/back.jpg";
IMPLEMENT_DYNAMIC(CMyVideoCon, CDialogEx)

CMyVideoCon::CMyVideoCon(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG3, pParent)
{

}

CMyVideoCon::~CMyVideoCon()
{
}

void CMyVideoCon::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);

	namedWindow("view", WINDOW_AUTOSIZE);
	HWND hWnd = (HWND)cvGetWindowHandle("view");
	HWND hParent = ::GetParent(hWnd);
	::SetParent(hWnd, GetDlgItem(IDC_DOC1)->m_hWnd);
	::ShowWindow(hParent, SW_HIDE);

	namedWindow("view1", WINDOW_AUTOSIZE);
	HWND hWnd1 = (HWND)cvGetWindowHandle("view1");
	HWND hParent1 = ::GetParent(hWnd1);
	::SetParent(hWnd1, GetDlgItem(IDC_DOC2)->m_hWnd);
	::ShowWindow(hParent1, SW_HIDE);

	namedWindow("view2", WINDOW_AUTOSIZE);
	HWND hWnd2 = (HWND)cvGetWindowHandle("view2");
	HWND hParent2 = ::GetParent(hWnd2);
	::SetParent(hWnd2, GetDlgItem(IDC_DOC3)->m_hWnd);
	::ShowWindow(hParent2, SW_HIDE);

	namedWindow("view3", WINDOW_AUTOSIZE);
	HWND hWnd3 = (HWND)cvGetWindowHandle("view3");
	HWND hParent3 = ::GetParent(hWnd3);
	::SetParent(hWnd3, GetDlgItem(IDC_DOC4)->m_hWnd);
	::ShowWindow(hParent3, SW_HIDE);
}


BEGIN_MESSAGE_MAP(CMyVideoCon, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CMyVideoCon::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, &CMyVideoCon::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON2, &CMyVideoCon::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON4, &CMyVideoCon::OnBnClickedButton4)
END_MESSAGE_MAP()


// CMyVideoCon 消息处理程序


BOOL CMyVideoCon::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}
String picpath;
void CMyVideoCon::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString picPath;   //定义图片路径变量  
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
		OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, NULL, this);   //选择文件对话框  

	if (dlg.DoModal() == IDOK)
	{
		picPath = dlg.GetPathName();  //获取图片路径  
	}
	//CString to string  使用这个方法记得字符集选用“使用多字节字符”，不然会报错  

	picpath = picPath.GetBuffer(0);
	VideoCapture capture(picpath);
	//zip.CalBackgroud(backfile);
	//int DoZip(picpath, zipfile, infofile);
	Ptr<BackgroundSubtractorMOG2> bg_model = createBackgroundSubtractorMOG2();
	Mat image;
	while (1)
	{
		capture >> image;
		if (!image.data)
		{
			cerr << "picture error!";
			return;
		}
		CRect rect;
		GetDlgItem(IDC_DOC1)->GetClientRect(&rect);
		Rect dst(rect.left, rect.top, rect.right, rect.bottom);
		resize(image, image, cv::Size(rect.Width(), rect.Height()));
		imshow("view", image);//原视频
		waitKey(30);
	}
	
}


void CMyVideoCon::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	zip.DoZip(picpath, zipfile, infofile);
	VideoCapture capture(zipfile);
	
	//int DoZip(picpath, zipfile, infofile);
	Ptr<BackgroundSubtractorMOG2> bg_model = createBackgroundSubtractorMOG2();
	Mat image;
	while (1)
	{
		capture >> image;
		if (!image.data)
		{
			cerr << "picture error!";
			return;
		}
		CRect rect;
		GetDlgItem(IDC_DOC1)->GetClientRect(&rect);
		Rect dst(rect.left, rect.top, rect.right, rect.bottom);
		resize(image, image, cv::Size(rect.Width(), rect.Height()));
		imshow("view1", image);//原视频
		waitKey(30);
	}
}


void CMyVideoCon::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	zip.SetVideoFile(picpath);
	zip.SetbackFile(backfile);
	zip.CalBackgroud(backfile);
	Mat img = imread(backfile);
	CRect rect;
	GetDlgItem(IDC_DOC3)->GetClientRect(&rect);
	Rect dst(rect.left, rect.top, rect.right, rect.bottom);
	resize(img, img, cv::Size(rect.Width(), rect.Height()));
	imshow("view2", img);
}


void CMyVideoCon::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	int wait;
	cv::Mat frame;

	// 视频还原播放
	zip.InitUnZip(zipfile, infofile);
	
	while (wait = zip.NextFrame(frame))
	{
		cv::waitKey(25 * wait);
		CRect rect;
		GetDlgItem(IDC_DOC4)->GetClientRect(&rect);
		Rect dst(rect.left, rect.top, rect.right, rect.bottom);
		resize(frame, frame, cv::Size(rect.Width(), rect.Height()));
		cv::imshow("view3", frame);
	}
	zip.CloseUnZip();
}
